package com.hckjsec.website.controller;

import java.util.List;

import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.website.domain.WebSiteCheckedroom;
import com.hckjsec.website.service.IWebSiteCheckedroomService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公众云选房Controller
 * 
 * @author jeethink
 * @date 2022-07-11
 */
@RestController
@RequestMapping("/system/checkedroom")
public class WebSiteCheckedroomController extends BaseController
{
    @Autowired
    private IWebSiteCheckedroomService webSiteCheckedroomService;

    /**
     * 查询公众云选房列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebSiteCheckedroom webSiteCheckedroom)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteCheckedroom.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteCheckedroom> list = webSiteCheckedroomService.selectWebSiteCheckedroomList(webSiteCheckedroom);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    /**
     * 导出公众云选房列表
     */
    @Log(title = "公众云选房", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebSiteCheckedroom webSiteCheckedroom)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteCheckedroom.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteCheckedroom> list = webSiteCheckedroomService.selectWebSiteCheckedroomList(webSiteCheckedroom);
        ExcelUtil<WebSiteCheckedroom> util = new ExcelUtil<WebSiteCheckedroom>(WebSiteCheckedroom.class);
        return util.exportExcel(list, "checkedroom");
    }

    /**
     * 获取公众云选房详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webSiteCheckedroomService.selectWebSiteCheckedroomById(id));
    }

    /**
     * 新增公众云选房
     */
    @Log(title = "公众云选房", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebSiteCheckedroom webSiteCheckedroom)
    {
        if(StringUtils.isNull(webSiteCheckedroom.getPublishTime())){
            webSiteCheckedroom.setPublishTime(DateUtils.getNowDate());
        }
        if (!"".equals(StringUtils.isHcNull(webSiteCheckedroom.getContent(),""))){//解密
            webSiteCheckedroom.setContent(Base64Converter.decode(webSiteCheckedroom.getContent()));
        }else{
            webSiteCheckedroom.setContent(webSiteCheckedroom.getContent());
        }
        webSiteCheckedroom.setDeptId(SecurityUtils.getDeptId());
        webSiteCheckedroom.setOperator(SecurityUtils.getUserId());
        return toAjax(webSiteCheckedroomService.insertWebSiteCheckedroom(webSiteCheckedroom));
    }

    /**
     * 修改公众云选房
     */
    @Log(title = "公众云选房", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebSiteCheckedroom webSiteCheckedroom)
    {
        if(StringUtils.isNull(webSiteCheckedroom.getPublishTime())){
            webSiteCheckedroom.setPublishTime(DateUtils.getNowDate());
        }
        if (!"".equals(StringUtils.isHcNull(webSiteCheckedroom.getContent(),""))){//解密
            webSiteCheckedroom.setContent(Base64Converter.decode(webSiteCheckedroom.getContent()));
        }else{
            webSiteCheckedroom.setContent(webSiteCheckedroom.getContent());
        }
        return toAjax(webSiteCheckedroomService.updateWebSiteCheckedroom(webSiteCheckedroom));
    }

    /**
     * 删除公众云选房
     */
    @Log(title = "公众云选房", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webSiteCheckedroomService.deleteWebSiteCheckedroomByIds(ids));
    }
}
